package com.desksoft.action;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.desksoft.common.PropertiesUtil;
import com.desksoft.common.UUIDGenerator;


/**
 * 
 * 批量上传图片
 *
 */
@Controller
@RequestMapping("/page")
public class UploadPicture {

	
	/**
	 * 上传
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/ajax/uploading",method=RequestMethod.POST)
	@ResponseBody
	public Object handleFormUpload(MultipartHttpServletRequest request)
			throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		List<MultipartFile> files =  request.getFiles("filedata");
		String diskPath = PropertiesUtil.getProperty("imageDiskPath");
		File f = new File(diskPath);
		if(!f.exists()){
			f.mkdirs();
		}
		String httpPath = PropertiesUtil.getProperty("imageHttpPath");
		String path = "";
		if(files != null && files.size() > 0){
			String fileName = UUIDGenerator.get() ;
			MultipartFile file = files.get(0);
			path = diskPath + File.separatorChar + fileName;
			if (file.isEmpty()){
				result.put("status", "1");
			}else {
				FileOutputStream fileOS = new FileOutputStream(path);
				fileOS.write(file.getBytes());
				fileOS.close();
				result.put("status", "1");
				result.put("imgUrl", httpPath + "/" + fileName );
			}
		}
		return result;
	}
}